package edu.neu.javaweb.service;

import edu.neu.javaweb.bean.User;
import edu.neu.javaweb.dao.UserDao;

public class UserService {
    UserDao userDao = new UserDao();

    public boolean canLogin(String username, String passwd, Object isLoginSession) {
        if (isLoginSession != null && (Boolean) isLoginSession) {
            // isLogin有，且为true，不用验证，认为已经登录
            System.out.println("已建立的会话 && 已登录: " + username);
        } else {
            if (isLoginSession == null) {
                System.out.println("初次见面: " + username);
            } else {
                System.out.println("已建立的会话 && 未登录: " + username);
            }
            // real judge
            if (userDao.getUserByNameAndPwd(username, passwd) == null) {
                return false;
            }
        }
        return true;
    }

    public boolean login(String username, String passwd) {
        if (userDao.getUserByNameAndPwd(username, passwd) == null) {
            return false;
        }
        return true;
    }

    public boolean addUser(String username, String passwd, String sex, String college) {
        UserDao userDao = new UserDao();
        if (userDao.findUser(username).size() == 0) {
            // 主键不重复，可以add
            return new UserDao().addUser(new User(username, passwd, sex, college));
        }
        return false;
    }
}
